package bavaria.hightec.proxylogging;


import java.lang.reflect.InvocationHandler;	
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

import bavaria.hightec.banking.BankCustomerView;



public class BankProxyInvocation implements InvocationHandler {
    private BankCustomerView bank;

    private static Logger logger = Logger.getLogger(BankProxyInvocation.class.getName());
    
    public BankProxyInvocation(BankCustomerView bank)  {
        this.bank = bank;
    }
    
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable  {
        
    	String a = ("* calling method " + method + " with params ");
        for (int i = 0; i < args.length; i++) 
            a += ((" " + args[i]));
        a += " \n ";
            
        if (method.getName() == "fund" && new Long(args[1].toString().substring(0, args[1].toString().length()-4)) >= 1000000)
    		logger.log(Level.INFO, "ATTENTION: account number " + args[0] + " received a high deposit of " + args[1].toString()
    				+ " " + args[2]);
        
        Object result = null;
        try  {
            result = method.invoke(bank, args);
        } catch(IllegalAccessException ex)  {
            ex.printStackTrace();
        } catch(InvocationTargetException ex)  {
            a += " \n " + ("* exception:" + ex.getTargetException());
            throw ex.getTargetException();
        }

        
        a +=("* result:" + result);
        

        logger.info(a);
        return result;
    }

}
